﻿package com.tudou.js
{
    import com.tudou.events.*;
    import flash.events.*;

    public class JSExchanger extends EventDispatcher
    {
        protected var shareDataMap:Object;
        protected const SET_DATA:String = "setData";
        protected const GET_DATA:String = "getData";
        protected var funMap:Object;
        public static var JS_NAME:String = "playerEx";
        private static var instance:JSExchanger;

        public function JSExchanger()
        {
            super(this);
            shareDataMap = {};
            funMap = {};
            JSCall.addCallback("notify", notifyHlr);
            return;
        }// end function

        public function getData(param1:String, param2:Function, param3:Function = null) : void
        {
            notifyJS(GET_DATA, [param1], param2, param3);
            return;
        }// end function

        private function notifyHlr(param1:String, param2:Array = null)
        {
            var _loc_3:Object = null;
            var _loc_4:String = null;
            var _loc_5:Function = null;
            if (param2 == null)
            {
                param2 = [];
            }
            if (param1 == SET_DATA)
            {
                _loc_4 = param2.shift();
                _loc_3 = shareDataMap[_loc_4];
                if (_loc_3)
                {
                    var _loc_6:* = param2[0];
                    _loc_3[_loc_4] = param2[0];
                    return _loc_6;
                }
                return null;
            }
            else
            {
                if (param1 == GET_DATA)
                {
                    _loc_4 = param2[0];
                    _loc_3 = shareDataMap[_loc_4];
                    if (_loc_3)
                    {
                        return _loc_3[_loc_4];
                    }
                    return null;
                }
                else
                {
                    if (funMap[param1] != null)
                    {
                        _loc_3 = funMap[param1] as Object;
                        _loc_5 = _loc_3[param1] as Function;
                        return _loc_5.apply(_loc_3, param2);
                    }
                    return dispatchEvent(new JSEvent(param1, param2));
                }
            }
        }// end function

        public function removeCallback(param1:String) : void
        {
            funMap[param1] = null;
            delete funMap[param1];
            return;
        }// end function

        public function setData(param1:String, param2) : void
        {
            notifyJS(SET_DATA, [param1, param2], null);
            return;
        }// end function

        public function addCallback(param1:Object, param2:String) : void
        {
            funMap[param2] = param1;
            return;
        }// end function

        public function shareData(param1:Object, param2:String) : void
        {
            shareDataMap[param2] = param1;
            return;
        }// end function

        public function killGetData(param1:String) : void
        {
            JSCall.killCall(this, JS_NAME + ".notify", [GET_DATA, param1]);
            return;
        }// end function

        public function notifyJS(param1:String, param2:Array = null, param3:Function = null, param4:Function = null) : void
        {
            if (param2 == null)
            {
                param2 = [param1];
            }
            else
            {
                param2.unshift(param1);
            }
            JSCall.call(this, JS_NAME + ".notify", param3, param2, param4);
            return;
        }// end function

        public function justGetData(param1:String)
        {
            return justNotifyJS(GET_DATA, [param1]);
        }// end function

        public function killNotifyJS(param1:String) : void
        {
            JSCall.killCall(this, JS_NAME + ".notify", [param1]);
            return;
        }// end function

        public function removeShare(param1:String) : void
        {
            shareDataMap[param1] = null;
            delete shareDataMap[param1];
            return;
        }// end function

        public function justNotifyJS(param1:String, param2:Array = null)
        {
            if (param2 == null)
            {
                param2 = [param1];
            }
            else
            {
                param2.unshift(param1);
            }
            return JSCall.justCall(JS_NAME + ".notify", param2);
        }// end function

        public static function getInst() : JSExchanger
        {
            var _loc_1:* = instance || new JSExchanger;
            instance = instance || new JSExchanger;
            return _loc_1;
        }// end function

    }
}
